package com.a16.yuejian.yuejian.fragment;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.a16.yuejian.yuejian.R;
import com.a16.yuejian.yuejian.activity.TrDetailActivity;
import com.a16.yuejian.yuejian.adapter.TrainAdapter;
import com.a16.yuejian.yuejian.javabean.Img;
import com.a16.yuejian.yuejian.javabean.Train;
import com.a16.yuejian.yuejian.listener.Listener;
import com.a16.yuejian.yuejian.model.TrainModel;
import com.oragee.banners.BannerView;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import static android.app.Activity.RESULT_OK;

public class TrainFragment extends Fragment implements View.OnClickListener{
    private TextView grade1,grade2,grade3,grade4;
    private List<View> viewList,id;
    BannerView bannerView;
    private RecyclerView recyclerView;
    private View view=null;

    Listener<Train> Tlistener = new Listener<Train>() {
        @Override
        public void onResponse(List<Train> t) {
            TrainAdapter trainAdapter = new TrainAdapter(getActivity());
            trainAdapter.setList(t);
            recyclerView.setAdapter(trainAdapter);
        }

        @Override
        public void onFail() {

        }
    };

    Listener<Img> listener = new Listener<Img>() {
        @Override
        public void onResponse(final List<Img> t) {
            viewList = new ArrayList<View>();
            for (int i = 0; i < t.size(); i++) {
                ImageView image = new ImageView(getActivity());
                image.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                //设置显示格式
                image.setScaleType(ImageView.ScaleType.FIT_XY);
                Picasso.with(getContext()).load("http://10.0.2.2:8080/neu_yuejian201/public/photo/"+t.get(i).getPic()).into(image);
                final String id =t.get(i).getTrainid().toString();
                viewList.add(image);
                image.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                            Intent intent = new Intent(getActivity(), TrDetailActivity.class);
                            intent.putExtra("trainid",id);
                            getActivity().startActivity(intent);
                    }
                });
            }
            bannerView.startLoop(true);
            bannerView.setViewList(viewList);
        }

        @Override
        public void onFail() {

        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        view=inflater.inflate(R.layout.fragment_train, container, false);
        TrainModel trainModel = new TrainModel();
        trainModel.alltrain("0",Tlistener);
        trainModel.lunbo(listener);
        initViews();
        initEvent();
        return view;
    }

    private void initEvent() {
        grade1.setOnClickListener(this);
        grade2.setOnClickListener(this);
        grade3.setOnClickListener(this);
        grade4.setOnClickListener(this);
    }

    private void initViews() {
        recyclerView = (RecyclerView)view.findViewById(R.id.rv_train);
        recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        grade1 = (TextView)view.findViewById(R.id.train_grade1);
        grade2 = (TextView)view.findViewById(R.id.train_grade2);
        grade3 = (TextView)view.findViewById(R.id.train_grade3);
        grade4 = (TextView)view.findViewById(R.id.train_grade4);
        bannerView = (BannerView)view.findViewById(R.id.train_banner);
    }


    @Override
    public void onClick(View v) {
        TrainModel trainModel = new TrainModel();
        switch (v.getId()){
            case R.id.train_grade1:
                trainModel.alltrain("0",Tlistener);
                break;
            case R.id.train_grade2:
                trainModel.alltrain("1",Tlistener);
                break;
            case R.id.train_grade3:
                trainModel.alltrain("2",Tlistener);
                break;
            case R.id.train_grade4:
                trainModel.alltrain("3",Tlistener);
                break;
        }
    }
}
